package z_map;

import java.awt.EventQueue;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

import javax.swing.JInternalFrame;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Font;
import javax.swing.JLabel;

public class MapList extends JInternalFrame {
	
	HashMap<String, MapData> map = new HashMap();
	
	private JTextField findStr;
	private JButton btnNewButton;
	private JButton btnNewButton_1;
	private JButton btnNewButton_2;
	private JScrollPane scrollPane;
	private JTextArea result;
	private JLabel status;


	/**
	 * Create the frame.
	 */
	public MapList(HashMap map ){
		super("List",true, true, true, false);
		this.map = map;
		setBounds(100, 100, 520, 400);
		getContentPane().setLayout(null);
		getContentPane().add(getFindStr());
		getContentPane().add(getBtnNewButton());
		getContentPane().add(getBtnNewButton_1());
		getContentPane().add(getBtnNewButton_2());
		getContentPane().add(getScrollPane());
		getContentPane().add(getStatus());

	}
	private JTextField getFindStr() {
		if (findStr == null) {
			findStr = new JTextField();
			findStr.setBounds(12, 10, 164, 21);
			findStr.setColumns(10);
		}
		return findStr;
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("조회");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					String find = findStr.getText();
					if(find.equals("")){
						result.append("검색어를 입력하세요.\n");
					}else{
						MapData d = map.get(find);
						if(d==null){
							result.append("찾는 데이터가 없습니다.\n");

						}else{
							result.append(d.toString2());
							result.append("\n");
							result.append("출력이 완료되었습니다.\n");
						}
					}
					status.setText("조히가 완료되었습니다.");
				}
			});
			btnNewButton.setBounds(183, 9, 97, 23);
		}
		return btnNewButton;
	}
	private JButton getBtnNewButton_1() {
		if (btnNewButton_1 == null) {
			btnNewButton_1 = new JButton("모든 키");
			btnNewButton_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					Set<String> set = map.keySet();
					Iterator<String> it = set.iterator();
					int cnt=0;
					
					result.setText("KEYS________________________\n");
					while(it.hasNext()){
						result.append(it.next());
						result.append("\n");
						cnt++;
					}
					
					result.append("총 건수 : " + cnt + "\n");
					status.setText("출력이 완료되었습니다.");
				}
			});
			btnNewButton_1.setBounds(285, 9, 97, 23);
		}
		return btnNewButton_1;
	}
	private JButton getBtnNewButton_2() {
		if (btnNewButton_2 == null) {
			btnNewButton_2 = new JButton("모든 데이터");
			btnNewButton_2.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					String str = 
							String.format("%-16s%-16s%-16s%-18s\n", 
										"아이디","성명","연락처","일자");
					result.setText(str);
					Collection<MapData > col  =  map.values();
					Iterator<MapData > it = col.iterator();
					
					while(it.hasNext()){
						MapData d = it.next();
						result.append(d.toString());
					}
					status.setText("출력이 완료되었습니다.");
				}
			});
			btnNewButton_2.setBounds(387, 9, 97, 23);
		}
		return btnNewButton_2;
	}
	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setBounds(12, 41, 472, 294);
			scrollPane.setViewportView(getResult());
		}
		return scrollPane;
	}
	private JTextArea getResult() {
		if (result == null) {
			result = new JTextArea();
			result.setFont(new Font("바탕체", Font.PLAIN, 12));
		}
		return result;
	}
	private JLabel getStatus() {
		if (status == null) {
			status = new JLabel("New label");
			status.setBounds(12, 345, 480, 15);
		}
		return status;
	}
}
